//
//  ICMineNetworkManager.m
//  ICome
//
//  Created by zhangrongwu on 16/9/26.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICContactsNetworkManager.h"
#import "ICRobotModel.h"
#import "ICHeaderFile.h"
@implementation ICContactsNetworkManager

+(instancetype)sharedInstance {
    static ICContactsNetworkManager *manager;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [[ICContactsNetworkManager alloc] init];
    });
    return manager;
}

- (void)getRobotListWithParam:(NSDictionary *)param
                 requestCache:(RequestCache)cache
                      success:(HttpRequestSuccess)success
                      failure:(HttpRequestFailed)failure
{
    NSString *url = [self appendRequestBaseURL:ROBOT_ROBOTLIST];
    [ICNetworkHelper POST:url parameters:param responseCache:^(id responseCache) {
        
        NSMutableArray *layoutList = [[NSMutableArray alloc] init];
        [layoutList addObjectsFromArray:[self robotWithDict:responseCache]];
        cache(layoutList);
        
    } success:^(id responseObject) {
        
        NSMutableArray *layoutList = [[NSMutableArray alloc] init];
        [layoutList addObjectsFromArray:[self robotWithDict:responseObject]];
        success(layoutList);
        
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (NSMutableArray *)robotWithDict:(NSDictionary *)dict {
    NSMutableArray *layoutList = [[NSMutableArray alloc] init];
    NSArray *findArray  = [dict objectForKey:@"data"];
    for (NSDictionary *dict in findArray) {
        ICRobotModel *model = [ICRobotModel mj_objectWithKeyValues:dict];
        [layoutList addObject:model];
    }
    return layoutList;
}


- (void)getRobInfoWithParam:(NSDictionary *)param
               requestCache:(RequestCache)cache
                    success:(HttpRequestSuccess)success
                    failure:(HttpRequestFailed)failure
{
    
    NSString *url = [self appendRequestBaseURL:ROBOT_ROBOTINFO];
    [ICNetworkHelper POST:url parameters:param responseCache:^(id responseCache){
        
        ICRobotModel *model = [self setRobInfoModelWithDict:responseCache];
        cache(model);
    }success:^(id responseObject){
    
        ICRobotModel *model = [self setRobInfoModelWithDict:responseObject];
        success(model);
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}

- (ICRobotModel *)setRobInfoModelWithDict:(NSDictionary *)dict{
    
    NSDictionary *tempDict = [dict objectForKey:@"data"];
    ICRobotModel *model = [ICRobotModel mj_objectWithKeyValues:tempDict];;
    return model;
}

@end
